package com.bruce.starts.demo.commonerror.jdk8;

import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @author luotuan
 * @Description
 * @create 2021-05-20 16:02
 **/
public class StreamDemo {
    public static void main(String[] args) {
//        IntStream.rangeClosed(1,100).parallel().forEach(i->{
//            System.out.println(LocalDateTime.now()+":"+i);
//            try {
//                TimeUnit.SECONDS.sleep(1);
//            }
//            catch (InterruptedException e) {
//            }
//        });
        //Stream.iterate(BigInteger.ZERO,a->a.add(BigInteger.valueOf(2L))).limit(10).forEach(System.out::println);
//        Stream.generate(()->"test").limit(3).forEach(System.out::println);
//        IntStream.range(0,3).mapToObj(i->"x"+i).forEach(System.out::println);
        //List<User> userList = IntStream.rangeClosed(1,3).mapToObj(i->new User("bruce"+i,22)).collect(Collectors.toList());
//        String ss = new Random().ints(48,122)
//                .filter(i->(i<57||i>65) && (i<90 || i>97))
//                .mapToObj(i->(char)i)
//                .limit(4)
//                .collect(StringBuilder::new,StringBuilder::append,StringBuilder::append)
//                .toString();
//        //System.out.println(ss);
//        final Integer[] integers = Lists.newArrayList(1, 2, 3, 4, 5)
//                .stream()
//                .collect(() -> new Integer[]{0}, (a, x) -> a[0] += x, (a1, a2) -> a1[0] += a2[0]);
//        Arrays.stream(integers).forEach(System.out::println);
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> map1 = new HashMap<>();
        map1.put("a1","aa1");
        map1.put("b2","bb2");
        Map<String,String> map2 = new HashMap<>();
        map2.put("a1","aa1");
        map2.put("b2","bb2");
        Map<String,String> map3 = new HashMap<>();
        map3.put("a3","aa3");
        map3.put("b3","bb3");
        list.add(map1);
        list.add(map2);
        list.add(map3);
//        List<Map<String,String>> result = list.stream().filter(oneObj->{
//            System.out.println(oneObj.size());
//            return CollectionUtils.isNotEmpty(
//                    oneObj.keySet().stream().filter(key->key.contains("a")).collect(Collectors.toList())
//            );
//                }
//        ).collect(Collectors.toList());
//        list.stream()
//
//                .collect(Collectors
//
//                        .collectingAndThen(Collectors
//
//                                .toCollection(()->), ArrayList::new));
//
        list.stream().flatMap(map->map.keySet().stream()).filter(key->key.contains("a")).map(m->m.toUpperCase()).collect(Collectors.toList());
//        list = list.stream().collect(ArrayList::new,(a,b)->{
//            a.add(b.entrySet().stream().filter(map->map.getKey().contains("a")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
//        },ArrayList::addAll);
        List result4 = list.stream().collect(LinkedList::new,(a,b)->{
            a.stream().forEach(map->{
                if(!map.equals(b)){
                    a.add(b);
                }
            });
        },LinkedList::addAll);

        List result5 = list.stream().distinct().collect(Collectors.toList());

        //System.out.println(map1.equals(map2));
        System.out.println("ssss");
        //compare();

    }

    private static void compare(){
        List<Map<String,Object>> list1 = new ArrayList<>();

        Map<String,Object> map = new HashMap();
        Map<String,Object> map2 = new HashMap();

        map.put("key","1");
        map.put("value","a");
        map2.put("key","2");
        map2.put("value","a");

        list1.add(map);
        list1.add(map2);
        list1.forEach(System.out::println);
        List<Map<String, Object>> list2 = list1.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(
                                () ->new TreeSet<>(Comparator.comparing(m->m.get("value").toString()))
                        ),ArrayList::new
                )
        );
        System.out.println("--------");
        list2.forEach(System.out::println);
    }
}
class User{
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public User setName(String name) {
        this.name = name;
        return this;
    }

    public int getAge() {
        return age;
    }

    public User setAge(int age) {
        this.age = age;
        return this;
    }
}
